<?php

namespace App\Http\Controllers\V1\Admin;

use App\Models\V1\SysPermission;
use App\Models\V1\SysRole;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RoleController extends BaseController
{
    /**
     * 角色列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function index(Request $request)
    {
        $query=SysRole::query();

        if($request->filled('title')){
            $query->where("title","like","%".$request->input('title')."%");
        }
        if($request->filled('role_key')){
            $query->where("role_key",$request->input('role_key'));
        }
        if($request->filled('state')){
            $query->where("state",$request->input('state'));
        }
        if($request->filled('begin_at')){
            $query->where("created_at",">=",$request->input('begin_at'));
        }
        if($request->filled('end_at')){
            $query->where("created_at","<=",$request->input('end_at'));
        }
        $data=$query->paginate($request->input('pageSize'));
        return ajax_output(200,"成功",$data);
    }

    /**
     * 更改角色状态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeStatus(Request $request)
    {
        $roleId=$request->input('roleId',0);
        $role=SysRole::find($roleId);
        if(!$role){
            return ajax_output(300,"数据不存在");
        }
        $role->update([
            'state'=>$request->input('state',1)
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 角色的权限
     * @param Request $request
     * @return void
     */
    public function permission(Request $request){
        $role_id=$request->input('role_id',0);
        $permission_model=new SysPermission();
        list($nodes,$permissions)=$permission_model->getPermissions($role_id);
        $data['checkedKeys']=$nodes;
        $data['menus']=$permission_model->getAllPermission();
        return ajax_output(200,"成功",$data);
    }

    /**
     * 创建角色
     * @param Request $request
     * @return void
     */
    public function store(Request $request)
    {
        try {
            DB::beginTransaction();
            $permissions=$request->input('menuIds',[]);
            $role_model=new SysRole();
            $role=$role_model->create([
                'title'=>$request->input('title',""),
                'role_key'=>$request->input('role_key',""),
                'sort'=>$request->input('sort',0),
                'state'=>(int)$request->input('state',1),
                'remark'=>$request->input('remark',null)
            ]);
            $permission_model=new SysPermission();
            !empty($permissions)&&$permission_model->setPermissions($permissions,$role->id);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return ajax_output(300,"数据异常:".$e->getMessage());
        }

        return ajax_output(200,"成功");
    }

    /**
     * 角色详情
     * @param $id
     * @return void
     */
    public function show($id)
    {
        $vo=SysRole::find($id);
        return ajax_output(200,"成功",$vo);
    }

    /**
     * 更新角色
     * @param Request $request
     * @param $id
     * @return void
     */
    public function update(Request $request, $id)
    {
        try {
            DB::beginTransaction();
            $permissions = $request->input('menuIds', []);
            $role_model = new SysRole();
            $role = $role_model->find($id);


            $role->update([
                'title' => $request->input('title', ""),
                'role_key' => $request->input('role_key', ""),
                'sort' => $request->input('sort', 0),
                'state' => (int)$request->input('state', 1),
                'remark' => $request->input('remark', null)
            ]);
            //更新权限
            $permission_model=new SysPermission();

            !empty($permissions)&&$permission_model->setPermissions($permissions,$role->id);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return ajax_output(300,"数据异常:".$e->getMessage());
        }
        return ajax_output(200,"成功");
    }

    /**
     * 删除角色
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        try {
            DB::beginTransaction();
            $ids = explode(",", $id);
            $role_model = new SysRole();
            $role_model->destroy($ids);
            //删除角色权限
            foreach ($ids as $role_id) {
                $permission_model=new SysPermission();
                $permission_model->delPermissions($role_id);
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return ajax_output(300,"数据异常:".$e->getMessage());
        }
        return ajax_output(200,"操作成功");
    }

    /**
     * 角色树选择
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function treeselect(Request $request){
        $role_model=new SysRole();
        $data=$role_model->where('state',1)->select('id as value','title as label')->get();
        return ajax_output(200,"成功",$data);
    }
}
